﻿using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using ArcEngine.Graphics;
using ArcEngine.Resources;
using WeifenLuo.WinFormsUI.Docking;

namespace ArcEngine.Editor
{
	public partial class TTFFontForm : DockContent
	{
		public TTFFontForm()
		{
			InitializeComponent();
			GlControl.InitializeContexts();
		}

		/// <summary>
		/// Initializes the form
		/// </summary>
		/// <returns></returns>
		public bool Init(TTFFont font)
		{
			CurrentFont = font;
			FontPropertyBox.SelectedObject = CurrentFont;
			TabText = font.Name;
			FontNameBox.Text = font.FileName;

			GlControl.MakeCurrent();
			Video.ShareVideoContext();
			Video.ClearColor = Color.Green;
			Video.Texturing = true;
			Video.Blending = true;


			// Preload background texture resource
			CheckerBoard = new Texture("CheckerBoard");
			Stream stream = ResourceManager.GetInternalResource("ArcEngine.Files.checkerboard.png");
			CheckerBoard.LoadImage(stream);
			stream.Close();


			UpdatePropertyBoxes();

			return true;
		}

		/// <summary>
		/// Update all property boxes
		/// </summary>
		private void UpdatePropertyBoxes()
		{

			SizeBox.Value = CurrentFont.Size;

			FontNameBox.Items.Clear();
			foreach (string name in ResourceManager.GetBinaries())
			{
				if (name.EndsWith(".ttf", true, CultureInfo.CurrentCulture))
					FontNameBox.Items.Add(name);
			}
			FontNameBox.SelectedText = CurrentFont.FileName;

			StyleBox.Items.Clear();
			StyleBox.DataSource = Enum.GetNames(typeof(FontStyle));
		}





		#region Properties

		/// <summary>
		/// Current font
		/// </summary>
		TTFFont CurrentFont = null;


		// Background texture
		Texture CheckerBoard;

		#endregion




		#region Events
		/// <summary>
		/// OnPaint
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GlControl_Paint(object sender, PaintEventArgs e)
		{
			DrawTimer.Stop();


			Video.ClearBuffers();

			// Background texture
			Video.Texture = CheckerBoard;
			Rectangle rect = new Rectangle(Point.Empty, GlControl.Size);
			Video.Blit(rect, rect);


			// Prints the text
			Video.Font = CurrentFont;
			Video.DrawText(Point.Empty, PreviewTextBox.Text);


			DrawTimer.Start();
		}


		/// <summary>
		/// Control resize
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GlControl_Resize(object sender, EventArgs e)
		{
			GlControl.MakeCurrent();
			Video.ViewPort = new Rectangle(new Point(), GlControl.Size);
		}


		/// <summary>
		/// OnClick, generate the font
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GenerateButton_Click(object sender, EventArgs e)
		{
			if (CurrentFont == null)
				return;

			FontStyle style = (FontStyle)Enum.Parse(typeof(FontStyle), (string)StyleBox.SelectedItem, true);
			CurrentFont.Generate(FontNameBox.Text, (int)SizeBox.Value, style);
		}

		/// <summary>
		/// Draw Timer
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DrawTimer_Tick(object sender, EventArgs e)
		{
			GlControl.Invalidate();
		}


		#endregion


	}
}
